package com.zhuchaooasystem.presenter.home.fragment.oa.record;

import android.app.Activity;

import com.google.gson.Gson;
import com.zhuchaooasystem.api.HttpApi;
import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpFile;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.AudioInfo;
import com.zhuchaooasystem.entity.RecordEntity;
import com.zhuchaooasystem.entity.UploadRecordEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class RecordPresenter extends BasePresenter<RecordContract.View> implements RecordContract.Presenter {

    public RecordPresenter(Activity activity, RecordContract.View view) {
        super(activity, view);
    }

    @Override
    public void pagerChanged(int position) {
        mView.setCurrentItem(position);
        mView.radioGroupChanged(position);
    }

    @Override
    public void getRecordList(final int index) {
        //----
        final int userId = UserInfo.getInstance().getMemberId();

        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("PersonnelPK", userId + "");
        map.put("PageIndex", index + "");
        Subscription subscription = HttpFactory.getHttpApi().getPronunciaTionList(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            RecordEntity entity = (RecordEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                if (index == 1) {
                                    mView.refreshData(entity.getResult().getSuccessful());
                                } else {
                                    mView.loadMoreData(entity.getResult().getSuccessful());
                                }
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void upload() {
        //----
        final int userId = UserInfo.getInstance().getMemberId();
        String username = UserInfo.getInstance().getUserName();
        String remark = mView.getRemark();
        String theme = mView.getThemeContent();
        List<AudioInfo> audioInfoList = mView.getAudioInfoList();
        mView.setClickable(false);
        showLoading();
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("userName", username);
        map.put("CusrmerMenber", userId + "");
        map.put("Theme", theme.trim());
        map.put("Remark", remark.trim());
        String often = "";
        List<String> files = new ArrayList<>();
        if (null != audioInfoList && audioInfoList.size() > 0) {
            for (int i = 0; i < audioInfoList.size(); i++) {
                often += audioInfoList.get(i).getOften();
                files.add(audioInfoList.get(i).getFilePath());
                if (i != audioInfoList.size() - 1) {
                    often += ",";
                }
            }
        }
        map.put("often", often);
        //----
        HttpFile.upload(mActivity, HttpApi.submitPronunciation, map, files, false, new HttpFile.HttpCallBack() {
            @Override
            public void onSusscess(String data) {
                try {
                    UploadRecordEntity entity = new Gson().fromJson(data, UploadRecordEntity.class);
                    if (null != entity && null != entity.getResult().getSuccessful()) {
                        mView.uploadSuccuss();
                    } else {
                        ToastUtil.showShort(mActivity, entity.getResult().getError());
                    }
                    LoadingDiaogDismiss();
                } catch (Exception e) {
                }
            }

            @Override
            public void onError(String meg) {
                super.onError(meg);
                mView.changeView();
                LoadingDiaogDismiss();
            }
        });

    }

}
